﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LuDo.PitEditor
{
    public partial class Sorting : Form
    {
        public string SortingString { get; private set; }
        public Sorting(string CurrentString)
        {
            InitializeComponent();
            UpdateGUI(CurrentString);
        }
        private void UpdateGUI(string CurrentString)
        {
            cmbSort1.SelectedIndex = -1;
            cmbSort2.SelectedIndex = -1;
            cmbSort3.SelectedIndex = -1;
            string[] record = CurrentString.Split(new char[] { ' ', ',' });
            int number = record.Count();
            int pos = 0;
            if (number >= 1)
            {
                string campo = record[pos];
                switch (campo)
                {
                    case "DataCreazione":
                        cmbSort1.SelectedIndex = 1;
                        break;
                    case "DataUpload":
                        cmbSort1.SelectedIndex = 2;
                        break;
                    case "Titolo":
                        cmbSort1.SelectedIndex = 3;
                        break;
                }
            }
            if (number >= 2)
            {
                string campo = record[pos + 1];
                if (campo == "DESC")
                {
                    rbDesc1.Checked = true;
                    pos = pos + 3;
                }
                else
                {
                    pos = pos + 2;
                }
            }
            if (number >= pos + 1)
            {
                string campo = record[pos];
                switch (campo)
                {
                    case "DataCreazione":
                        cmbSort2.SelectedIndex = 1;
                        break;
                    case "DataUpload":
                        cmbSort2.SelectedIndex = 2;
                        break;
                    case "Titolo":
                        cmbSort2.SelectedIndex = 3;
                        break;
                }
            }
            if (number >= pos + 1)
            {
                string campo = record[pos + 1];
                if (campo == "DESC")
                {
                    rbDesc2.Checked = true;
                    pos = pos + 3;
                }
                else
                {
                    pos = pos + 2;
                }
            }
            if (number >= pos + 1)
            {
                string campo = record[pos];
                switch (campo)
                {
                    case "DataCreazione":
                        cmbSort3.SelectedIndex = 1;
                        break;
                    case "DataUpload":
                        cmbSort3.SelectedIndex = 2;
                        break;
                    case "Titolo":
                        cmbSort3.SelectedIndex = 3;
                        break;
                }
            }
            if (number >= pos + 2)
            {
                string campo = record[pos + 1];
                if (campo == "DESC")
                {
                    rbDesc3.Checked = true;
                }
            }
        }
        private string GeneraStringa()
        {
            string srt = "";
            int index1 = cmbSort1.SelectedIndex;
            int index2 = cmbSort2.SelectedIndex;
            int index3 = cmbSort3.SelectedIndex;
            switch (index1)
            {
                case 0:
                    break;
                case 1:
                    srt += rbAsc1.Checked ? "DataCreazione" : "DataCreazione DESC";
                    break;
                case 2:
                    srt += rbAsc1.Checked ? "DataUpload" : "DataUpload DESC";
                    break;
                case 3:
                    srt += rbAsc1.Checked ? "Titolo" : "Titolo DESC";
                    break;
            }
            switch (index2)
            {
                case 0:
                    break;
                case 1:
                    srt += rbAsc2.Checked ? ", DataCreazione" : ", DataCreazione DESC";
                    break;
                case 2:
                    srt += rbAsc2.Checked ? ", DataUpload" : ", DataUpload DESC";
                    break;
                case 3:
                    srt += rbAsc2.Checked ? ", Titolo" : ", Titolo DESC";
                    break;
            }
            switch (index3)
            {
                case 0:
                    break;
                case 1:
                    srt += rbAsc3.Checked ? ", DataCreazione" : ", DataCreazione DESC";
                    break;
                case 2:
                    srt += rbAsc3.Checked ? ", DataUpload" : ", DataUpload DESC";
                    break;
                case 3:
                    srt += rbAsc3.Checked ? ", Titolo" : ", Titolo DESC";
                    break;
            }
            return srt;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.SortingString = GeneraStringa();
            if (cbDefault.Checked)
            {
                PitEditor.Properties.Settings.Default.DefaultSort = this.SortingString;
                PitEditor.Properties.Settings.Default.Save();
            }
        }

        //Static methods
        public static string ExplainSort(string CurrentString)
        {
            string str = "";
            string[] record = CurrentString.Split(new char[] { ' ', ',' });
            int number = record.Count();
            int pos = 0;
            if (number >= 1)
            {
                string campo = record[pos];
                switch (campo)
                {
                    case "DataCreazione":
                        str = "Per data di creazione";
                        break;
                    case "DataUpload":
                        str = "Per data di pubblicazione";
                        break;
                    case "Titolo":
                        str = "Per titolo";
                        break;
                }
            }
            if (number >= 2)
            {
                string campo = record[pos + 1];
                if (campo == "DESC")
                {
                    str += " discendente";
                    pos = pos + 3;
                }
                else
                {
                    pos = pos + 2;
                }
            }
            if (number >= pos + 1)
            {
                string campo = record[pos];
                switch (campo)
                {
                    case "DataCreazione":
                        str += ", per data di creazione";
                        break;
                    case "DataUpload":
                        str += ", per data di pubblicazione";
                        break;
                    case "Titolo":
                        str += ", per titolo";
                        break;
                }
            }
            if (number >= pos + 1)
            {
                string campo = record[pos + 1];
                if (campo == "DESC")
                {
                    str += " discendente";
                    pos = pos + 3;
                }
                else
                {
                    pos = pos + 2;
                }
            }
            if (number >= pos + 1)
            {
                string campo = record[pos];
                switch (campo)
                {
                    case "DataCreazione":
                        str += ", per data di creazione";
                        break;
                    case "DataUpload":
                        str += ", per data di pubblicazione";
                        break;
                    case "Titolo":
                        str += ", per titolo";
                        break;
                }
            }
            if (number >= pos + 2)
            {
                string campo = record[pos + 1];
                if (campo == "DESC")
                {
                    str += " discendente";
                }
            }
            return str;
        }
    }
}
